<template>
  <div class="app-container">
    <a-form :model="queryParams" ref="queryRef" v-show="showSearch" layout="inline">
      <a-form-item label="用户名称" field="userName">
        <a-input
            size="small"
            v-model="queryParams.userName"
            placeholder="请输入用户名称"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </a-form-item>
      <a-form-item label="手机号码" field="phonenumber">
        <a-input
            size="small"
            v-model="queryParams.phonenumber"
            placeholder="请输入手机号码"
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" class="button-margin-right" size="small" @click="handleQuery">
          <icon-search/>
          搜索
        </a-button>
        <a-button @click="resetQuery">
          <icon-loop/>
          重置
        </a-button>
      </a-form-item>
    </a-form>

    <a-row :gutter="10" class="mb8">
      <a-col :span="1.5">
        <a-button
            type="primary"
            size="small"
            @click="openSelectUser"
            v-hasPermi="['system:role:add']"
        >
          <icon-plus/>
          添加用户
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            type="primary"
            status="danger"
            size="small"
            :disabled="multiple"
            @click="cancelAuthUserAll"
            v-hasPermi="['system:role:remove']"
        >
          <icon-close-circle/>
          批量取消授权
        </a-button>
      </a-col>
      <a-col :span="1.5">
        <a-button
            size="small"
            @click="handleClose"
        >
          <icon-close/>
          关闭
        </a-button>
      </a-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </a-row>

    <a-table v-loading="loading" :data="userList" row-key="userId" :row-selection="userRowSelection"
             v-model:selectedKeys="selectedUserIds" :pagination="false">
      <template #columns>
        <a-table-column title="用户名称" data-index="userName" :show-overflow-tooltip="true"/>
        <a-table-column title="用户昵称" data-index="nickName" :show-overflow-tooltip="true"/>
        <a-table-column title="邮箱" data-index="email" :show-overflow-tooltip="true"/>
        <a-table-column title="手机" data-index="phonenumber" :show-overflow-tooltip="true"/>
        <a-table-column title="状态" align="center" data-index="status">
          <template #cell="{ record }">
            <dict-tag :options="sys_normal_disable" :value="record.status"/>
          </template>
        </a-table-column>
        <a-table-column title="创建时间" align="center" data-index="createTime" width="180">
          <template #cell="{ record }">
            <span>{{ parseTime(record.createTime) }}</span>
          </template>
        </a-table-column>
        <a-table-column title="操作" align="center" class-name="small-padding fixed-width">
          <template #cell="{ record }">
            <el-button
                type="text"
                @click="cancelAuthUser(record)"
                v-hasPermi="['system:role:remove']"
            ><icon-close-circle/>取消授权
            </el-button>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <SelectUser ref="selectRef" :roleId="queryParams.roleId" @ok="handleQuery"/>
  </div>
</template>

<script setup name="AuthUser">
import {allocatedUserList, authUserCancel, authUserCancelAll} from "@/api/system/role";
import {computed, getCurrentInstance, reactive, ref} from "vue";
import {useRoute} from "vue-router";
import SelectUser from "./selectUser.vue";

const route = useRoute();
const {proxy} = getCurrentInstance();
const {sys_normal_disable} = proxy.useDict("sys_normal_disable");

const userList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId,
  userName: undefined,
  phonenumber: undefined,
});

/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  allocatedUserList(queryParams).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 返回按钮
function handleClose() {
  const obj = {path: "/system/role"};
  proxy.$tab.closeOpenPage(obj);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
const selectedUserIds = ref([]);
const userRowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
  onlyCurrent: false,
});
// 批量取消授权按钮生效
const multiple = computed(() => {
  return !selectedUserIds.value.length
});

/** 打开授权用户表弹窗 */
function openSelectUser() {
  proxy.$refs['selectRef'].show();
}

/** 取消授权按钮操作 */
function cancelAuthUser(row) {
  proxy.$confirm('确认要取消该用户"' + row.userName + '"角色吗？').then(function () {
    return authUserCancel({userId: row.userId, roleId: queryParams.roleId});
  }).then(() => {
    getList();
    proxy.$message.success("取消授权成功");
  }).catch(() => {
  });
}

/** 批量取消授权按钮操作 */
function cancelAuthUserAll(row) {
  const roleId = queryParams.roleId;
  const uIds = selectedUserIds.value.join(",");
  proxy.$confirm("是否取消选中用户授权数据项?").then(function () {
    return authUserCancelAll({roleId: roleId, userIds: uIds});
  }).then(() => {
    getList();
    proxy.$message.success("取消授权成功");
  }).catch(() => {
  });
}

getList();
</script>
